//Component Object
import { request } from '../../utils/request.js'
const app = getApp()
let openid = ''
let unionid = ''
let custPhone = ''
Component({
  properties: {
    show:{
      type:Boolean,
      observer: function(newVal, oldVal) {
        // console.log('login Dailog:', newVal)
      }
    },
    url:{
      type:'String',
      value:'/pages/index/index'
    }
  },
  data: {
  },
  methods: {
    getPhoneNumber(e) {
      console.log('getPhoneNumber e.detail:',e.detail)
      wx.showLoading({
        title: '登录中'
      })
      if (e.detail.errMsg == 'getPhoneNumber:ok') {
      var that = this
      // 获取微信手机号
      request({
        url: '/wechatApp/phone',
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          code: e.detail.code,
          appid: app.globalData.appid,
        },
        method: 'GET',
        success: function (respData) {
          // console.log('phone respData:',respData)
          custPhone = respData.phoneNumber
          wx.setStorageSync('custPhone', respData.phoneNumber)
          wx.login({
            success: (loginRes) => {
              if (loginRes.code) {
                request({
                  url: '/wechatApp/getOpenId',
                  data: {
                    code: loginRes.code,
                    appid: app.globalData.appid,
                  },
                  method: 'GET',
                  success: function (respData) {
                    // console.log('getOpenId respData:', respData)
                    openid = respData.openid
                    unionid = respData.unionid
                    wx.setStorageSync('openid', openid)
                    wx.setStorageSync('unionid', unionid)
                    that.serverLogin()
                   
                  },
                  fail: function (err) {
                    wx.hideLoading()
                    wx.showToast({
                      title: '获取openid失败',
                      icon: 'none'
                    })
                  }
                })
              } else {
              }
            },
          })
        },
        fail: function (err) {
          wx.hideLoading()
        }
      })
      }else {
        wx.hideLoading()
        wx.showToast({
          title: '获取手机号失败',
          icon: 'none'
        })
      }
    },
    // 注册和更新
    serverLogin () {
      let that = this
      var info = {
        openId: openid,
        custPhone: custPhone,
        unionId: unionid
      }
      request({
        url: '/api/users/mp-user-login',
        data: info,
        method: 'post',
        success: (respData,data)=> {
          let token = data.token
          wx.setStorageSync('token', token)
          wx.hideLoading()
          wx.showToast({
            title: '登录成功',
            icon: 'success'
          })
          let url = that.data.url
          let launchOptions = wx.getEnterOptionsSync()
          if(launchOptions && launchOptions.query){
            let queryStr = ''
            for(let key in launchOptions.query){
              queryStr += key + '=' + launchOptions.query[key] + '&'
            }
            url = url + '?' + queryStr
          }
          
          // this.triggerEvent('loginSuccess')
          wx.reLaunch({
            url: url,
            success:()=>{
              wx.showToast({
                title: '登录成功',
                icon: 'success'
              })
            }
          })
        },
        fail: function (err) {
          wx.hideLoading()
          wx.showToast({
            title: '登录失败请稍后再试',
            icon: 'none'
          })
        }
      })
    },
    weChatLogin () {
      const that = this
      request({
        url:'/api/CustBasic/userOpenIdLogin/' + uerInfo.openId,
        data: {},
        method: 'post',
        success: function (respData,originData) {
          // console.log('userOpenIdLogin respData:', respData)
          // console.log('userOpenIdLogin originData:', originData)
          wx.hideLoading()
          wx.setStorageSync('token', originData.token)
          wx.showToast({
            title: '登录成功',
            icon: 'success'
          })
        },
        fail: function (err) {
          wx.hideLoading()
          wx.showToast({
            title: '登录失败',
            icon: 'none'
          })
        },
      })
    },
    dialogClose(){
      console.log('dialogClose')
      this.triggerEvent('close')
    },
    agreePrivacyAuthorization(){
      console.log('同意了用户隐私协议1')
      this.triggerEvent('agreePrivacyAuthorization')
    }
  }
});